/*
 ============================================================================
 Page	     : 421-422
 Name        : addaword.c
 Date	     : 20210407
 Author      : Hbue 18060333
 Copyright   : Your copyright notice
 Description : 使用fprintf()、fscanf()和rewind()
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41

int main(void)
{
	FILE *fp;
	char words[MAX];

	if((fp = fopen("wordy", "a+")) == NULL)
	{
		fprintf(stdout,"Can't open \"wordy\" file.\n");
		exit(EXIT_FAILURE);
	}

	puts("Enter words to add to the file; ");
	puts("press the # key at the beginning of a line to terminate.");
	while((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#'))
	{
		fprintf(fp, "%s\n", words);
	}
	puts("File contents:");
	rewind(fp);				/* 返回到文件开始处 */
	while(fscanf(fp, "%s", words) == 1)
	{
		puts(words);
	}
	puts("Done!");
	if(fclose(fp) != 0)
	{
		fprintf(stderr, "Error closing file\n");
	}

	return 0;
}

